<template>
   <el-card>
   <el-table border :data="skuList" v-loading="loading">
    <el-table-column  
      label='序号'
      type='index'
      align='center'
      width="80px"
      > 
    </el-table-column>
    <el-table-column label="名称" prop="skuName"></el-table-column>
    <el-table-column label="描述" prop="skuDesc"></el-table-column>
    <el-table-column label="默认图片">
      <template v-slot="{row}">
          <el-image :src="row.skuDefaultImg" style="width:100px"></el-image>
        </template>
    </el-table-column>
    <el-table-column label="重量(KG)" prop="weight"></el-table-column>
    <el-table-column label="价格(元)" prop="price"></el-table-column>
    <el-table-column label="操作" width="280px" align="center">
     <template v-slot="{row,$index}">
       <el-tooltip content="上架" placement="top" >
        <el-button type="success" v-show="row.isSale!==0" :icon="Top" size="small" @click="toOnSale(row)"></el-button>
          </el-tooltip>
          <el-tooltip content="下架" placement="top">
            <el-button type="danger" v-show="row.isSale===0" :icon="Bottom" size="small" @click="toCancelSale(row)" ></el-button>
          </el-tooltip>
          <el-tooltip content="修改SKU" placement="top">
            <el-button type="primary" :icon="Edit" size="small" ></el-button>
          </el-tooltip>
          <el-tooltip content="查看详情" placement="top">
            <el-button type="info" :icon="InfoFilled" size="small" @click="toCheckSkuInfo(row)"></el-button>
          </el-tooltip>

          <el-popconfirm width="220" confirm-button-text="确定" cancel-button-text="No, Thanks" :icon="InfoFilled" icon-color="#626AEF" title="您真的要删除当前Sku数据么?" @confirm="toDeleteSkuInfo(row,$index)" @cancel="cancelEvent">
            <template #reference>
              <el-button size="small" type="danger" :icon="Delete"></el-button>
            </template>
          </el-popconfirm>
     </template>
    </el-table-column>
   </el-table>

     <!-- 分页器 -->
     <el-pagination 
     style="margin-top:10px"
     v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[3,6,9,12]" :small="small" :disabled="disabled" :background="background" layout=" prev, pager, next, jumper, ->,sizes,total" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />

     <el-drawer class="skuInfo" size="50%" v-model="drawerVisible" title="查看skuInfo" direction="rtl">
      <el-row :gutter="20">
        <el-col :span="6">
          <h4>名称</h4>
        </el-col>
        <el-col :span="18">
          <span>{{skuInfo.skuName}}</span>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="6">
          <h4>描述</h4>
        </el-col>
        <el-col :span="18">
          <span>{{ skuInfo.skuDesc }}}</span>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="6">
          <h4>价格</h4>
        </el-col>
        <el-col :span="18">
          <span>{{skuInfo.price}}.00</span>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="6">
          <h4>平台属性</h4>
        </el-col>
        <el-col :span="18">
          <el-tag class="ml-2" style="margin-right:4px" v-for="attrValue in skuInfo.skuAttrValueList" type="success" :key="attrValue.id">{{attrValue.valueName}}</el-tag>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="6">
          <h4>销售属性</h4>
        </el-col>
        <el-col :span="18">
          <el-tag class="ml-2" style="margin-right:4px" v-for="saleAttrValue in skuInfo.skuSaleAttrValueList" type="success" :key="saleAttrValue.id">{{saleAttrValue.saleAttrValueName}}</el-tag>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="6">
          <h4>商品图片</h4>
        </el-col>
        <el-col :span="18">
          <el-carousel :interval="2000">
            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
              <el-image :src="item.imgUrl"></el-image>
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>

   </el-card>
</template>

<script lang="ts">
export default {
  name: "Sku",
};
</script>

<script lang="ts" setup>
import { ref,onMounted} from 'vue'
import { Plus, Edit, Delete, InfoFilled, Top, Bottom } from '@element-plus/icons-vue';
import { reqGetAllSkuListData,reqSkuOnSale,reqSkuCancelSale,reqDeleteSkuInfo,reqGetSkuInfoData } from '@/api/product/sku'
import { ElMessage } from 'element-plus'

const currentPage = ref(1);
const pageSize = ref(10);
const skuList = ref([]);
const total = ref(0);
const small = ref(false);
const loading = ref(true);
const background = ref(false);
const disabled = ref(false);
const skuInfo = ref({})
const drawerVisible = ref(false)

//定义获取skuList的数据的函数
const getAllSkuListData = async()=>{
  loading.value = true
  const res = await reqGetAllSkuListData(currentPage.value,pageSize.value)
  loading.value = false
  skuList.value = res.records
  total.value = res.total
}

onMounted(()=>{
  getAllSkuListData()
})

//每页数量
const handleSizeChange = (val:number)=>{
  pageSize.value = val
  currentPage.value = 1 
  getAllSkuListData()  
}


//页码
const handleCurrentChange=(val:number)=>{
 currentPage.value = val
 getAllSkuListData()  
  
}

//上架
const toOnSale = async row =>{
  await reqSkuOnSale(row.id)
  ElMessage.success('上架成功')
  row.isSale = 1 - row.isSale
}

//下架
const toCancelSale = async row =>{
  await reqSkuCancelSale(row.id)
  ElMessage.success('下架成功')
  row.isSale = 1 - row.isSale
}

//删除skuInfo商品
const toDeleteSkuInfo = async (row, index)=>{
  await reqDeleteSkuInfo(row.id)
  ElMessage.success('删除sku成功')
   // 使用剩下的数据重新渲染页面
   skuList.value.splice(index, 1);
}

//取消删除
const cancelEvent = () => {
  ElMessage.info('取消了删除...');
};

const toCheckSkuInfo = async row =>{
  const res = await reqGetSkuInfoData(row.id)
  skuInfo.value = res
  drawerVisible.value = true
}

</script>

<style scoped></style>
